package com.example.shop

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop.databinding.ActivityRegisterBinding

class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun initView() {
        binding.btnRegister.setOnClickListener {
            viewModel.register(binding.etUsername.text.toString(),
            binding.etPassword.text.toString(),
            binding.etRepassword.text.toString())
        }
    }

    override fun initData() {
        viewModel.registerSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){//注册成功
                //设置结束的响应码 和跳转的页面及传值内容
                setResult(102, Intent().putExtra("username",it.data.username).putExtra("password",it.data.password))
                finish()//结束当前界面
            }
        }
        viewModel.registerFail.observe(this){
            ToastUtils.showLong(it)

        }
    }

    override fun getViewModel(): Class<RegisterViewModel>  = RegisterViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_register

}